<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="卡号：" name="code">
				<a-input :disabled="formData.id" v-model:value="formData.code" placeholder="请输入卡号" allow-clear />
			</a-form-item>
			<a-form-item label="卡类型：" name="cardType">
				<a-select
					:disabled="formData.id"
					v-model:value="formData.cardType"
					:options="cardTypeOptions"
					placeholder="请选择卡类型"
					allow-clear></a-select>
			</a-form-item>
			<a-form-item label="开卡组织：" name="storeCode">
				<a-select
					v-model:value="formData.storeCode"
					:options="storeOptions"
					placeholder="请选择开卡组织"/>
			</a-form-item>
			<a-form-item label="卡状态：" name="status">
				<a-select
					:disabled="formData.id"
					v-model:value="formData.status"
					:options="cardStatusOptions"
					placeholder="请选择卡状态"></a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmCardTypeForm">
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			status: '0',
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.cardType += ''
			formData.value.status += ''
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入卡编码')],
		status: [required('请选择卡状态')],
		cardType: [required('请选择卡类型')],
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	let cardStatusOptions = proxy.$TOOL
		.dictTypeList('CARD_STATUS')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let cardTypeOptions = tool
		.tableList('CRM_CARD_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	let storeOptions = tool
		.tableList('ORG_CATEGORY')
		.map(item => {
			return {
				value: item['code'],
				label: item['name']
			}
		})
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = parameterChanges(formData.value)
				submitLoading.value = true
				crmApi.cardSubmitForm(param, param.id).then(res => {
					visible = false
					message.success(res.msg)
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}
	const parameterChanges = (data) => {
		if (!data.component) {
			return data
		}
		// 如果用户输入的组件path路径
		if (data.component.slice(0, 1) === '/') {
			data.component = data.component.slice(1)
		}
		return data
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
